package gr.handmademarket.ui.pages;

import gr.handmademarket.bl.operations.UploadProductOperation;
import gr.handmademarket.po.implementations.Product;
import gr.handmademarket.po.interfaces.Category;
import gr.handmademarket.po.interfaces.User;
import gr.handmademarket.ui.WebHandMadeMarketApplication;
import gr.handmademarket.ui.WebHandMadeSession;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletContext;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.form.upload.FileUpload;
import org.apache.wicket.markup.html.form.upload.FileUploadField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;


/**
 * page for uploading content.
 */
//@AuthorizeInstantiation("SELLER")
public class UploadProductPage 
extends CommonTemplatePage{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * the form model.
	 */
	private IModel<Product> model = new CompoundPropertyModel<Product>(new Product());
	
	/**
	 * the form.
	 */
	@SuppressWarnings("nls")
	private Form<Product> form = new Form<Product>("pform",model);
	
	/**
	 * file upload field.
	 */
	@SuppressWarnings("nls")
	private FileUploadField fileUploadField = new FileUploadField("fileUpload");
	
	/**
	 * the name field.
	 */
	@SuppressWarnings("nls")
	private TextField<String> nameField = new RequiredTextField<String>("name"); 
	
	/**
	 * the name field.
	 */
	@SuppressWarnings("nls")
	private TextField<Double> priceField = new TextField<Double>("price", Double.class); 
	
	/**
	 * the description field.
	 */
	@SuppressWarnings("nls")
	private TextArea<String> descriptionField = new TextArea<String>("description");
	
	/**
	 * the availability field.
	 */
	@SuppressWarnings("nls")
	private CheckBox availabilityField = new CheckBox("availability");

	/**
	 * categories combo choice renderer. 
	 */
	@SuppressWarnings("nls")
	private ChoiceRenderer<Category> renderer = new ChoiceRenderer<Category>("name", "code");
	
	/**
	 * the categories combo.
	 */
	@SuppressWarnings("nls")
	private DropDownChoice<Category> categoryChoice =
		new DropDownChoice<Category>("category", new ArrayList<Category>(), renderer);
	
	/**
	 * submit link.
	 */
	@SuppressWarnings("nls")
	private AjaxButton submit = new AjaxButton("submitBtn", form) {

		/**
		 * serialVersionUID.
		 */
		private static final long serialVersionUID = 1L;

		@Override
		protected void onSubmit(AjaxRequestTarget target, Form<?> form1) {
			UploadProductOperation operation = new UploadProductOperation();
			Product p = form.getModelObject();
			User user = ((WebHandMadeSession) WebHandMadeSession.get()).getUser();
			String username = user != null ?  user.getUsername() : null;
			//p.setSeller(WebHandMadeMarketApplication.getSellerByUsername(username));
			operation.setProduct(p);
			operation.execute();
			p = operation.getProduct();
			ServletContext context = WebHandMadeMarketApplication.get().getServletContext();
			String realPath = context.getRealPath("/images/products/" + p.getCode());
			File folder = new File(realPath);
			folder.mkdir();
			FileUpload fileUpload =  fileUploadField.getFileUpload();
			if (fileUpload != null){	
				//File imageFile = new File(folder,fileUpload.getClientFileName());
				File imageFile = new File(folder,"1.jpg");
				try {
					imageFile.createNewFile();
					fileUpload.writeTo(imageFile);
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
			setResponsePage(HomePage.class);
		}
		
		@Override
		protected void onError(AjaxRequestTarget target, Form<?> form1) {
			target.add(UploadProductPage.this.get("feedbackPanel"));
		}
		
	};
	
	/**
	 * Creates a new UploadPage object. 
	 *
	 */
	public UploadProductPage(){
		super();
		add(form);
		descriptionField.setRequired(true);
		availabilityField.setRequired(true);
		priceField.setRequired(true);
		form.add(nameField,priceField,availabilityField,descriptionField,categoryChoice,fileUploadField,submit);
	}
	
}
